<?php
	class the_model_class extends the_model_meta {
		function get_class($query,$data=null) {return oh_model_get_class($query,$data);}
		function old_get_class($query = "") {
			if (!$this->test()) return oh_this();					

			$a = $this->get_args($query,"name");
			$p = $this->get_prefix();
			$w = $this->get_where($a);
			$q = "SELECT * FROM {$p}class $w";
			$r = oh($this->datasource)->get_results($q);
			$c = count($r);

			if ($c > 0) {
				foreach($r as &$i) {					
					$i->fields = $this->parse_model_form($i->form);
				}
				return $r[0];
			}									
		}
		
		function set_class($query = "") {
		}
		function new_class($query="",$data="") {return oh_model_new_class($query,$data);}
		function old_new_class($query = "") {
			global $oh_object_model;
			if (!$this->test()) return oh_this();

			$args = $this->get_args($query,"name","define:form");			
			$args->form = $this->sanitize_model_form($args->form);			

			$prefix = $this->get_prefix();
			$i 		= $this->get_class_insert($args);
			$fields = $i["fields"];
			$values = $i["values"];
			if (!$this->hit_class($args)) {
				$q = "INSERT INTO {$prefix}class($fields) VALUES ($values)";
				oh($this->datasource)->query($q);					
			}		
			return $oh_object_model;
		}
		
		function hit_class($query,$data=null) {return oh_model_hit_class($query,$data);}
		
		function old_hit_class($query = "") {
			if ($this->test_installation()) {
				$args 	= $this->get_args($query,"name");
				$prefix = $this->get_prefix();				
				$w 		= $this->get_where($args);
				$q = "SELECT id FROM {$prefix}class $w LIMIT 1";
				$c = oh($this->datasource)->get_results($q);
				return count($c) > 0;
			}
		}
		
	
	
			
		function sanitize_model_form($formcode) {
			$formcode = preg_replace("/[ \r\t\n]/","",$formcode);
			$formcode = explode(",",$formcode);
			$formcode_output = array(); 
			foreach($formcode as $linecode) {
				$linecode = explode(":",$linecode);
				$type = trim($linecode[0]);
				$name = trim($linecode[1]);
				if ($name) {
					$op	= $type{0};
					if (($op!="!")||($op!="^")||($op!="_")) {
						if (false) {
						} else if (in_array($type,$this->model_object_fields["real"])) {
							$op = "!";
						} else if (in_array($type,$this->model_object_fields["meta"])) {
							$op = "_";						
						} else {
							$op = "^";						
						}
						
					} else {
						$op = "";
					}
					$mu = $name{0};
					if (($mu!="*")||($mu!="*")) {
						$mu = ".";
					} else {
						$mu = "*";
					}
					$formcode_output[] = $op.$type.':'.$mu.$name;
				}
			}
			$formcode_output = implode(",",$formcode_output);
			return $formcode_output;		
		}
		function parse_model_form($formcode) {
			$o = array();
			$o['real'] = array();
			$o['meta'] = array();
			$o['chil'] = array();
			$o['tyof'] = array();
						
			$formcode = explode(",",$formcode);
			foreach($formcode as $linecode) {
				$linecode = explode(":",$linecode);
				$type = $linecode[0];
				$name = $linecode[1];
				$op   = $type{0};
				$mu   = $name{0};
				$type = substr($type,1);
				$name = substr($name,1);
				if (false) {
				} else if ($op=="!") {
					$o['real'][$type] = $name;
				} else if ($op=="_") {
					$o['meta'][$type] = $name;
				} else if ($op=="^") {
					$o['chil'][$type] = $name;
				}
				$o['tyof'][$name] = $type;
			}
			return $o;
		}
	
	}
?>
